{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.16010688]\n",
      " [ 0.25115308]\n",
      " [ 0.24380908]\n",
      " [-0.03734478]]\n",
      "[[-0.19240416 -0.04857844  0.9397631  -0.72788469]\n",
      " [ 0.00418431 -0.87457364  0.67660843  0.46940443]\n",
      " [ 0.19852014  0.00450072  0.72784584 -0.72311168]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,0,0],\n",
    "            [1,0,1],\n",
    "            [1,1,0],\n",
    "            [1,1,1]])\n",
    "Y = np.array([[0,1,1,0]])\n",
    "\n",
    "lr = 0.11 #学习率\n",
    "V = np.random.random((3,4))*2-1 #获取随机的隐藏层权值（范围在0-1）\n",
    "W = np.random.random((4,1))*2-1 #获取随机的输出层权值（范围在0-1）\n",
    "print(W)\n",
    "print(V)\n",
    "\n",
    "def sigmoid(x): \n",
    "    return 1/(1+np.exp(-x))\n",
    "def dsigmoid(x): #sigmoid函数求导的结果即f(x)=f'(x)[1-f(x)]\n",
    "    return x*(1-x)\n",
    "\n",
    "def update():  #更新函数\n",
    "    global X,Y,W,V,lr\n",
    "    L1 = sigmoid(np.dot(X,V))  #隐藏层输出\n",
    "    L2 = sigmoid(np.dot(L1,W)) #输出层输出\n",
    "    \n",
    "    L2_delta = (Y.T-L2)*dsigmoid(L2) #输出层的误差信号\n",
    "    L1_delta = L2_delta.dot(W.T)*dsigmoid(L1) #隐藏层的误差信号\n",
    "    \n",
    "    W_C = lr*L1.T.dot(L2_delta)#输出层的权值改变量\n",
    "    V_C = lr*X.T.dot(L1_delta) #隐藏层的权值改变量\n",
    "    \n",
    "    W = W + W_C\n",
    "    V = V + V_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: 0.499413815154\n",
      "Error: 0.498528174514\n",
      "Error: 0.49324468108\n",
      "Error: 0.45332677662\n",
      "Error: 0.324616688925\n",
      "Error: 0.15486259026\n",
      "Error: 0.0966918046831\n",
      "Error: 0.0733661530208\n",
      "Error: 0.0607027925737\n",
      "Error: 0.0526205238551\n",
      "Error: 0.0469424135728\n",
      "Error: 0.0426943794466\n",
      "Error: 0.0393725752196\n",
      "Error: 0.0366886810241\n",
      "Error: 0.0344649797595\n",
      "Error: 0.0325855096993\n",
      "Error: 0.0309711211454\n",
      "Error: 0.0295657837742\n",
      "Error: 0.028328619603\n",
      "Error: 0.0272290401545\n",
      "[[ 0.01404561]\n",
      " [ 0.96919752]\n",
      " [ 0.97462049]\n",
      " [ 0.03475077]]\n"
     ]
    }
   ],
   "source": [
    "for i in range(20000):\n",
    "    update()\n",
    "    if i % 1000 == 0: \n",
    "        L1 = sigmoid(np.dot(X,V))\n",
    "        L2 = sigmoid(np.dot(L1,W))\n",
    "        print(\"Error:\",np.mean(np.abs(Y.T-L2))) #输出误差值\n",
    "L1 = sigmoid(np.dot(X,V))\n",
    "L2 = sigmoid(np.dot(L1,W))\n",
    "print(L2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
